﻿using System.Web;
using System.Web.SessionState;

using SY.WebSite.Utility;

namespace SY.WebSite.Web
{
    /// <summary>
    /// Web Handler基类
    /// </summary>
    public class BaseWebHandler : IRequiresSessionState, IHttpHandler
    {
        /// <summary>
        /// http请求
        /// </summary>
        protected HttpRequest Request;
        /// <summary>
        /// http输出
        /// </summary>
        protected HttpResponse Response;

        private string action;
        
        public virtual void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            Request = context.Request;
            Response = context.Response;
        }

        /// <summary>
        /// 获取当前的Action值
        /// </summary>
        protected string Action
        {
            get
            {
                if (string.IsNullOrEmpty(action))//2010-07-29做一个调整，用一个变量保存
                    action = Request.QueryString["action"] ?? "";

                return action;
            }
        }

        #region json转换统一入口 2010-11-18

        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ToJson(object obj)
        {
            return JsonTransfer.ToJson(obj);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public static T DeserializeObject<T>(string value)
        {
            return JsonTransfer.DeserializeObject<T>(value);
        }

        #endregion

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        public virtual void BreakResponse(string message = "参数异常，请刷新页面再重试")
        {
            Response.Write(new JsonTransfer().SetError(message));
            Response.End();
        }
    }
}